import * as _ from "lodash"

function isPrimitive(value: any) {
  return _.isNumber(value) || _.isString(value)
    || _.isUndefined(value) || _.isNull(value) || _.isEmpty(value)
}

function caseTransform(value: any, key: any, keyCase: (name: string) => string) {
  if (isPrimitive(value)) {
    return value
  }
  if (_.isArray(value)) {
    return _.map(value, (item, itemKey) => caseTransform(item, itemKey, keyCase))
  }
  return _.transform(value, (result, value, key: string) => {
    if (isPrimitive(value)) {
      result[keyCase(key)] = value
    } else if (_.isArray(value)) {
      result[keyCase(key)] = _.map(value, (item, itemKey) => caseTransform(item, itemKey, keyCase))
    } else {
      result[keyCase(key)] = caseTransform(value, undefined, keyCase)
    }
  }, {})
}

export function snakeCase(source: any) {
  return caseTransform(source, undefined, _.snakeCase)
}

export function camelCase(source: any) {
  return caseTransform(source, undefined, _.camelCase)
}
